package cn.taskservice.common.exception;

import cn.taskservice.common.enums.ResultCode;
import lombok.Getter;

/**
 * 自定义异常
 */
@Getter
public class ServiceException  extends RuntimeException{
    private final int code;

    public ServiceException(ResultCode code) {
        super(code.getMessage());
        this.code = code.getCode();
    }

    public ServiceException(ResultCode code, Throwable cause) {
        super(code.getMessage(), cause);
        this.code = code.getCode();
    }

    public ServiceException(int code, String message) {
        super(message);
        this.code = code;
    }

    public ServiceException(int code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
    }

    public ServiceException(String message) {
        super(message);
        this.code = ResultCode.SERVER_ERROR.getCode(); // 或者默认 500
    }

    public ServiceException(String message, Throwable cause) {
        super(message, cause);
        this.code = ResultCode.SERVER_ERROR.getCode();
    }

    public ServiceException(Throwable cause) {
        super(cause);
        this.code = ResultCode.SERVER_ERROR.getCode();
    }

    public static void throwIf(boolean condition, ResultCode code) {
        if (condition) throw new ServiceException(code);
    }
}
